Single Byte XOR Cipher

See http://cryptopals.com/sets/1/challenges/3


In [ ]:
cipherhex = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
cipherbytes = hex2bytes(cipherhex);

In [ ]:
function xorByte(x::UInt8, cb::Array{UInt8,1})
    pt = cb $ x
    return pt
end;

In [ ]:
function englishiness(ptarray::Array{UInt8, 1})
    EnglishFrequencies = Dict( # from http://scottbryce.com/cryptograms/stats.htm
    'e'=>12.51, 
    't'=>9.25,
    'a'=>8.04,
    'o'=>7.6,
    'i'=>7.26,
    'n'=>7.09,
    's'=>6.54,
    'r'=>6.12,
    'h'=>5.49,
    'l'=>4.14,
    'd'=>3.99,
    'c'=>3.06,
    'u'=>2.71,
    'm'=>2.53,
    'f'=>2.3,
    'p'=>2.0,
    'g'=>1.96,
    'w'=>1.92,
    'y'=>1.73,
    'b'=>1.54,
    'v'=>0.99,
    'k'=>0.67,
    'x'=>0.19,
    'j'=>0.16,
    'q'=>0.11,
    'z'=>0.09,
    )
    s=0
    for ll in ptarray
        llchar = Char(ll)
        if isalpha(llchar)
            s+=get(EnglishFrequencies, lowercase(llchar), -5)
        else
            s+=-20
        end
    end
    return s
end;

In [ ]:
decrypts = []
for xx in 0x00:0xff
    ptbytes = xorByte(xx, cipherbytes)
    ptscore = englishiness(ptbytes)
    push!(decrypts, (xx, ptbytes, ptscore))
end

In [ ]:
sort!(decrypts, rev=true, lt=(x,y)->x[3]<y[3]);

In [ ]:
String(decrypts[1][2])